package cn.iocoder.yudao.module.security.controller.admin.communicate;

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.security.controller.admin.communicate.vo.*;
import cn.iocoder.yudao.module.security.dal.dataobject.communicate.SecurityCommunicateDO;
import cn.iocoder.yudao.module.security.service.communicate.SecurityCommunicateService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 通信行为")
@RestController
@RequestMapping("/security/communicate")
@Validated
public class SecurityCommunicateController {

    @Resource
    private SecurityCommunicateService communicateService;

    @PostMapping("/create")
    @Operation(summary = "创建通信行为")
    @PreAuthorize("@ss.hasPermission('security:communicate:create')")
    public CommonResult<Long> createCommunicate(@Valid @RequestBody SecurityCommunicateSaveReqVO createReqVO) {
        return success(communicateService.createCommunicate(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新通信行为")
    @PreAuthorize("@ss.hasPermission('security:communicate:update')")
    public CommonResult<Boolean> updateCommunicate(@Valid @RequestBody SecurityCommunicateSaveReqVO updateReqVO) {
        communicateService.updateCommunicate(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除通信行为")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('security:communicate:delete')")
    public CommonResult<Boolean> deleteCommunicate(@RequestParam("id") Long id) {
        communicateService.deleteCommunicate(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得通信行为")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('security:communicate:query')")
    public CommonResult<SecurityCommunicateRespVO> getCommunicate(@RequestParam("id") Long id) {
        SecurityCommunicateDO communicate = communicateService.getCommunicate(id);
        return success(BeanUtils.toBean(communicate, SecurityCommunicateRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得通信行为分页")
    @PreAuthorize("@ss.hasPermission('security:communicate:query')")
    public CommonResult<PageResult<SecurityCommunicateRespVO>> getCommunicatePage(@Valid SecurityCommunicatePageReqVO pageReqVO) {
        PageResult<SecurityCommunicateDO> pageResult = communicateService.getCommunicatePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SecurityCommunicateRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出通信行为 Excel")
    @PreAuthorize("@ss.hasPermission('security:communicate:export')")
    @OperateLog(type = EXPORT)
    public void exportCommunicateExcel(@Valid SecurityCommunicatePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SecurityCommunicateDO> list = communicateService.getCommunicatePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "通信行为.xls", "数据", SecurityCommunicateRespVO.class,
                        BeanUtils.toBean(list, SecurityCommunicateRespVO.class));
    }

}